package com.xique.system.controller;

import com.xique.common.core.enums.StatusCode;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.system.bean.request.UploadImageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shd
 * @create 2022/1/21 15:54
 */
@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController {

    @Autowired
    private AliossUtil aliossUtil;

    @PostMapping(value = "/uploadImage")
    public AjaxResult uploadImage(UploadImageRequest request) {

        Map<String, Object> map = aliossUtil.reduceImageSizeAndUpload(request.getImage(), 100);

        if (StatusCode.SUCCESS.getCode() != map.get("code")) {
            return AjaxResult.error("上传图片失败！！！");
        }

        Map<String, Object> result = new HashMap<>();
        result.put("key", map.get("objectName"));
        result.put("url", map.get("url"));
        return AjaxResult.success("成功", result);
    }

}
